#include "CEnemy.h"

CBulletInfo::CBulletInfo(): maOffset(CPoint(0, 0)), iKeyFrame(-1), iLifeTime(-1), iType(-1){}

CBulletInfo::CBulletInfo(CPoint inMaOffset, int inIKeyFrame, int inIType, int inILifeTime):
	maOffset(inMaOffset), iKeyFrame(inIKeyFrame), iType(inIType), iLifeTime(inILifeTime){}

CBulletInfo::CBulletInfo(const CBulletInfo* inCBulletInfo): maOffset(inCBulletInfo->maOffset), 
	iKeyFrame(inCBulletInfo->iKeyFrame), iType(inCBulletInfo->iType), iLifeTime(inCBulletInfo->iLifeTime){}

void CEnemy::FreeArrBulletInfo(){
	for(vector<CBulletInfo*>::iterator tempItor = arrBulletInfo.begin(); tempItor != arrBulletInfo.end(); tempItor++){
		delete *tempItor;
		*tempItor = NULL;
	}
	arrBulletInfo.clear();
}

CEnemy::CEnemy(): iType(-1), CAlive(){}

CEnemy::CEnemy(int inIType, int inIHP, CMath* inMaArea, float inFSpeedX, float inFSpeedY, int inIPower, 
	vector<ALLEGRO_BITMAP*>::const_iterator begin, vector<ALLEGRO_BITMAP*>::const_iterator end, CPoint inMaCoordinate):
	iType(inIType), CAlive(inIHP, inMaArea, inFSpeedX, inFSpeedY, inIPower, begin, end, inMaCoordinate){}

CEnemy::CEnemy(const CEnemy& inCEnemy): iType(inCEnemy.iType), CAlive(inCEnemy){
	for(vector<CBulletInfo*>::const_iterator tempItor = inCEnemy.arrBulletInfo.begin(); tempItor != inCEnemy.arrBulletInfo.end(); 
		tempItor++){
			arrBulletInfo.push_back(new CBulletInfo(*(*tempItor)));
	}
}


CEnemy& CEnemy::operator=(const CEnemy& inCEnemy){
	if(this != &inCEnemy){
		iType = inCEnemy.iType;
		CAlive::operator=(inCEnemy);
		FreeArrBulletInfo();
		for(vector<CBulletInfo*>::const_iterator tempItor = inCEnemy.arrBulletInfo.begin(); tempItor != inCEnemy.arrBulletInfo.end(); 
			tempItor++){
				arrBulletInfo.push_back(new CBulletInfo(*tempItor));
		}
	}
	return *this;
}

CEnemy::~CEnemy(){
	FreeArrBulletInfo();
}

vector<CBulletInfo*>& CEnemy::GetArrBulletInfo(){
	return arrBulletInfo;
}

void CEnemy::AddBulletInfo(const CBulletInfo* inCBulletInfo){
	arrBulletInfo.push_back(new CBulletInfo(inCBulletInfo));
}

void CEnemy::SetArrBulletInfo(const vector<CBulletInfo*>& inArrBulletInfo){
	FreeArrBulletInfo();
	arrBulletInfo = inArrBulletInfo;
}

int CEnemy::GetIBulletCounter(){
	return iBulletCounter;
}

void CEnemy::SetIBulletCounter(int inIBulletCirculation){
	iBulletCounter = inIBulletCirculation;
}

int CEnemy::GetIBulletCirculation(){
	return iBulletCirculation;
}

void CEnemy::SetIBulletCirculation(int inIBulletCirculation){
	iBulletCirculation = inIBulletCirculation;
}

bool CEnemy::GetActiveBullets(vector<CBulletInfo*>::iterator& begin, vector<CBulletInfo*>::iterator& end){
	bool result;
	if(arrBulletInfo[iArrBulletInfoItorLeft]->iKeyFrame == iBulletCounter || iArrBulletInfoItorLeft == -1){
		begin = arrBulletInfo.begin() + iArrBulletInfoItorLeft;
		end = arrBulletInfo.begin() + iArrBulletInfoItorRight;
		result = true;
	}
	else
		result = false;
	return result;
}

int CEnemy::FindInfoRight(int left){
	int i;
	if(left == -1)
		i = -1;
	else
		for(i = left; ; i++){
			if (i == arrBulletInfo.size() || arrBulletInfo[i]->iKeyFrame != arrBulletInfo[left]->iKeyFrame)
				break;
		}
	return i;
}

void CEnemy::UpdateEnemyBullet(){
	if(!arrBulletInfo.empty() && iArrBulletInfoItorLeft != -1){
		if(iArrBulletInfoItorRight == arrBulletInfo.size()){
			iArrBulletInfoItorLeft = iBulletCirculation;
			iArrBulletInfoItorRight = FindInfoRight(iArrBulletInfoItorLeft);
			if(iArrBulletInfoItorLeft != -1)
				iBulletCounter = arrBulletInfo[iArrBulletInfoItorLeft]->iKeyFrame - 1;
		}
		else if(arrBulletInfo[iArrBulletInfoItorRight + 1]->iKeyFrame == iBulletCounter){
			iArrBulletInfoItorLeft = iArrBulletInfoItorRight;
			iArrBulletInfoItorRight = FindInfoRight(iArrBulletInfoItorLeft);
		}
		iBulletCounter++;
	}
}

void CEnemy::ResetBulletInfo(){
	if(!arrBulletInfo.empty()){
		iBulletCounter = 0;
		iArrBulletInfoItorLeft = 0;
		iArrBulletInfoItorRight = FindInfoRight(iArrBulletInfoItorLeft);
	}
}
